package com.xiangmo.travel.Controller;

import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.LoginDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Dto.UserDto;
import com.xiangmo.travel.Dto.UserUpdateDto;
import com.xiangmo.travel.Dvo.OtherUserDvo;
import com.xiangmo.travel.Dvo.TableCollectDvo;
import com.xiangmo.travel.Entity.User;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Service.PersonalityWallService;
import com.xiangmo.travel.Service.UserService;
import com.xiangmo.travel.Utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户模块
 * 包括：登录、注册
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private PersonalityWallService personalityWallService;

    /**
     * 用户注册
     * @param userDto 邮箱、账号、密码
     * @return 执行状态
     */
//    @ApiOperation("用户注册")@ApiParam("用户信息")
    @PostMapping("/register")
    public ResultData<User> register(@RequestBody UserDto userDto) {
        User user = userService.checkUser(userDto.getUsername());
        if(user != null){
            return new ResultData<>(CodeAndMsg.USER_NAME_ERROR.getCode(),
                    CodeAndMsg.USER_NAME_ERROR.getMessage(), null, null);
        }else if(userService.register(userDto) != 1){
            return new ResultData<>(CodeAndMsg.USER_REGISTER_ERROR.getCode(),
                    CodeAndMsg.USER_REGISTER_ERROR.getMessage(), null, null);
        }
        return new ResultData<User>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(),null, null);
    }

    /**
     * 后期要加验证码！
     * @param loginDto
     * @return 返回用户
     */
    @PostMapping("/login")
    public ResultData<User> getStateByUser(@RequestBody LoginDto loginDto){
        User user = null;
        System.out.println("进入login");
        String token = null;
        String userId = userService.userLogin(loginDto);
        if(userId == null){
            return new ResultData<User>(CodeAndMsg.LOGIN_PASSWORD_ERROR.getCode(),
                    CodeAndMsg.LOGIN_PASSWORD_ERROR.getMessage(),null, null);
        }else if(userId.equals("usernameError")){
            return new ResultData<User>(CodeAndMsg.LOGIN_NAME_ERROR.getCode(),
                    CodeAndMsg.LOGIN_NAME_ERROR.getMessage(),null, null);
        }else{
            token = tokenUtil.generateToken(userId);
            user = userService.checkUser(loginDto.getUsername());
        }
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), user, token);
    }
    //退出登录
    @TokenRequired
    @GetMapping("/login/out")
    public ResultData<User> outLoginById(@RequestHeader("Authorization") String token){
        tokenUtil.deleteToken(token);
        return new ResultData<>(CodeAndMsg.LOGIN_OUT_SUCCESS.getCode(),
                CodeAndMsg.LOGIN_OUT_SUCCESS.getMessage(),null, null);
    }

    //通过token获取用户信息
    @TokenRequired
    @GetMapping("/userInfo")
    public ResultData<User> getUserInfoByToken(@RequestHeader("Authorization") String token){
        Long userId = tokenUtil.getTokenValue(token);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(),userService.getUserById(userId), token.substring(7));
    }

    //用户修改个人信息
    @TokenRequired
    @PutMapping("/userInfo")
    public ResultData<Integer> setUserInfo(@RequestBody UserUpdateDto userUpdateDto, @RequestHeader("Authorization") String token){
        int status = userService.setUserInfo(userUpdateDto);
        if (status <= 0) {
            return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                    CodeAndMsg.ERROR.getMessage(),null, token.substring(7));
        }
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(),null, token.substring(7));
    }

//    跳转其他用户的个人页面信息==============
    @TokenRequired
    @GetMapping("/userInfo/{userId}/{likesId}")
    public ResultData<OtherUserDvo> getOtherUserInfo(@PathVariable Long userId, @PathVariable Long likesId, @RequestHeader("Authorization") String token){
        OtherUserDvo otherUserDvo = userService.getOtherUserInfo(userId, likesId);
        if (otherUserDvo == null) {
            return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                    CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
        }
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), otherUserDvo, token.substring(7));
    }

    //其他用户对指定用户进行 取消点赞
    @TokenRequired
    @DeleteMapping("/userLike/{userId}/{likesId}")
    public ResultData<Integer> deleteUserLike(@PathVariable Long userId, @PathVariable Long likesId, @RequestHeader("Authorization") String token){
        int status = userService.deleteUserLike(userId, likesId);
        if (status == 0) {
            return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                    CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
        }
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
    }

    //其他用户对指定用户进行 点赞
    @TokenRequired
    @GetMapping("/userLike/{userId}/{likesId}")
    public ResultData<Integer> addUserLike(@PathVariable Long userId, @PathVariable Long likesId, @RequestHeader("Authorization") String token){
        int status = userService.addUserLike(userId, likesId);
        if (status <= 0) {
            return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                    CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
        }
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
    }

    //echarts 信誉分
    @TokenRequired
    @GetMapping("/userLike/echarts/{userId}")
    public ResultData<List<TableCollectDvo>> getEchartsCreditPoints(@PathVariable Long userId, @RequestHeader("Authorization") String token){
        List<TableCollectDvo> echartsData =  userService.getCreditPoints(userId);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), echartsData, token.substring(7));
    }
    //echarts 活跃度
    @TokenRequired
    @GetMapping("/userLike/echarts/active/{userId}")
    public ResultData<List<TableCollectDvo>> getEchartsActive(@PathVariable Long userId, @RequestHeader("Authorization") String token){
        List<TableCollectDvo> echartsData =  userService.getActive(userId);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), echartsData, token.substring(7));
    }

    //后期有时间就添加个性墙的功能

}
